home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5450 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  58 lines

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help for a float problem
  5. Date: 9 Feb 1996 22:28 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <9FEB199622285820@erich.triumf.ca>
  9. References: <AMCHAVAN.96Jan25112007@ac2.hq.eso.org> <4f6tt6$rt1@news.cencom.net> <311A83D1.57A3@feith.com> <4fe4m4$f1c@news.ee.net> <4fgbss$nqm@ixnews6.ix.netcom.com>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4fgbss$nqm@ixnews6.ix.netcom.com>, wzjn@ix.netcom.com (KPN ) writes...
  14. >Good Afternoon,
  15. >Using æHow To Program C (second edition)Æ by Deitel & Deitel, IÆm
  16. >trying to do on page 92, example 3.17. IÆm not sure, but believe that
  17. >my problem is where to use a æfloatÆ. 
  18. >I want to ask the user to enter gallons used, which of course, could be
  19. >12.8. However, with the same line, I want the user to enter æ-1Æ if
  20. >they choose to end the program.
  21. >The way it is written so far, the user can successfully exit the
  22. >program with a -1. The user can also successfully enter whole numbers
  23. >and get an answer. My problem arises when I enter (again) 12.8 ... the
  24. >program goes in to an infinate loop! eh?
  25. >Conversely, if I define gallons as a float, the program does not
  26. >recognize the æ-1Æ as an end of program. eh? 
  27.  
  28. If you expect the user to enter a decimal value like 12.8 for gallons (or
  29. miles), you must declare gallons as float.
  30.  
  31. However, when the user enters -1 to quit, you will be caught by the fact that
  32. floating point numbers are never exact (Although I would expect -1 to be), so
  33. it is unwise to compare two floats for equality.
  34.  
  35. You could compare their difference to some small number, or, for this problem,
  36. simply consider any negative number as the signal to quit.
  37.  
  38.  
  39. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  40. Internet: bennett@triumf.ca         | of one another only when one can be
  41. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  42. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  43. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.